package com.jianyun.jyzs.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.adapter.PDAdapter;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.bean.PDDetial;
import com.jianyun.jyzs.bean.TimeEngineer;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.dao.EngLocationDao;
import com.jianyun.jyzs.dao.EngineerDao;
import com.jianyun.jyzs.http.PDHttpHelper;
import com.jianyun.jyzs.utils.DateTime;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jrmf360.rylib.common.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeSet;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by mlli on 2017/11/11.
 */

public class OthersDyamicActivity extends AppCompatActivity implements View.OnClickListener {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    private EngineerDao dao;
    private PDAdapter adapter;
    private List<TimeEngineer> mDatas;
    private String otherUserid;
    private Unbinder bind;
    private String enterpriseCode;
    private String userId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //UtilsStyle.setStatusBarMode(this,true);
        setContentView(R.layout.ac_personal_dyamic);
        bind = ButterKnife.bind(this);
        init();
    }



    protected void init() {
        topBack.setVisibility(View.VISIBLE);
        topBack.setOnClickListener(this);
        topText.setVisibility(View.VISIBLE);
        topText.setText("个人动态");
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        userId = LoginCache.getInstance().getUserInfo().getUserId();
        //得到个人userID notnull
        otherUserid = getIntent().getStringExtra(SysConstant.msg_key_string1);
        Log.i("test","otherUserid:"+otherUserid);
        dao = new EngineerDao(this);
        mDatas = new ArrayList<>();
        List<EngineeringResult.EngineerBean> engineer = dao.getEngineerByUser2(otherUserid, enterpriseCode);
        //Log.i("test","别人动态："+engineer.size());
        //Log.i("test","别人动态："+engineer.toString());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PDAdapter(this, null,userId);
        if (engineer.size()>0){
            adapter.setUserName(engineer.get(0).getUserName());
            String photo = engineer.get(0).getPhoto();
            String headPath=LoginCache.getInstance().getLoginCache().getErpRootUrl()+photo;
            adapter.setHeadImgPath(headPath);
        }
        recyclerView.setAdapter(adapter);
        transformTimerShaft(engineer);
        initData();
    }

    private TreeSet<String> timeTag = new TreeSet<>();
    private List<String> timeList = new ArrayList<>();
    private String tag = "tag";

    //转换数据适配个人动态适配器
    private void transformTimerShaft(List<EngineeringResult.EngineerBean> engineer) {
        if (engineer != null) mDatas.clear();
        //先得到日期标签 共三次遍历恶心  如果有更好的方法请替换
        for (EngineeringResult.EngineerBean ee : engineer) {
            String day = DateTime.YMD(ee.getDtLong());
            if (timeList.contains(day)){
                continue;
            }else {
                timeList.add(day);
            }
        }

        RceLog.d("lml",timeList.toString());
        for (String s:timeList) {
            ArrayList<EngineeringResult.EngineerBean> list = new ArrayList<>();
            for (EngineeringResult.EngineerBean ee : engineer) {

                if (DateTime.YMD(ee.getDtLong()).equals(s)) {
                    list.add(ee);
                }

            }
            mDatas.add(new TimeEngineer(list, s, 1));
        }
        int count = new EngLocationDao(this).getEngineerCount();
        TimeEngineer timeEngineer = new TimeEngineer(null, null, 0);
        PDDetial detial = new PDDetial();
        detial.setPart(count);
        detial.setUploadCount(dao.getEngineerCount(otherUserid));
        detial.setUserId(otherUserid);
        timeEngineer.setPdDetial(detial);
        mDatas.add(0, timeEngineer);

        adapter.setList(mDatas, 2);

    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("method","GePersonalProjectProgress");
        map.put("userId", userId);
        map.put("enterpriseCode", enterpriseCode);
        map.put("otherUserId", otherUserid);
        map.put("appCode", "jyzs");
        map.put("appType", "Android");
        map.put("appVersion", "1.0.2");
        PDHttpHelper.getPersonalDynamic(dao, map, new PDHttpHelper.HttpResultListener() {
            @Override
            public void resultListener(boolean b, String msg) {
                if (b) {
                    List<EngineeringResult.EngineerBean> engineer = dao.getEngineerByUser2(otherUserid, enterpriseCode);
                    transformTimerShaft(engineer);
                } else {
                    ToastUtil.showNoWaitToast(OthersDyamicActivity.this, msg);
                }
            }
        });
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.topBack:
                finish();
                break;
        }
    }
}
